package com.tianlan.blog.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.tianlan.blog.dao.SysRoleDao;
import com.tianlan.blog.dao.SysUserRoleDao;
import com.tianlan.blog.entity.SysRole;
import com.tianlan.blog.entity.SysUserRole;
import com.tianlan.blog.service.SysUserRoleService;

@Service
public class SysUserRoleServiceImpl implements SysUserRoleService {

	@Autowired
	private SysUserRoleDao sysUserRoleDao;
		
	@Autowired
	private SysRoleDao sysRoleDao;

	@Override
    @Cacheable(value = "roles", key = "'user_id_'.concat(#userId)")
	public List<SysRole> queryRolesByUserId(Integer userId) {
		
		List<SysUserRole> userRoleList =  sysUserRoleDao.findByUserIdAndDelFlag(userId,"0");
        if (!CollectionUtils.isEmpty(userRoleList)) {
            List<Integer> roleIds = new ArrayList<Integer>();
            for (SysUserRole sysUserRole : userRoleList) {
                roleIds.add(sysUserRole.getRoleId());
            }
            List<SysRole> roleList = sysRoleDao.getSysRoleList(roleIds);
            return roleList;
        }else{
        	return null;
        }
	}
}
